/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.process;

import cn.com.jsepc.rep.comm.Common;
import cn.com.jsepc.rep.comm.ConstUtil;
import com.jarp.db.DAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

/**
 *
 * @author 赵远
 */
public class RelateRep {
    Common com=new Common();
    public String relate_process(String array,String id){
        
        String [] myid=id.split("_");
        id=myid[myid.length-1];
        String res="";
        DAO dao=new DAO();
//        String delete="delete from tb_process_rep where processid="+id;
//        
//        try {
//            dao.execCommand(ConstUtil.DBNAME, delete);
//        } catch (ServletException ex) {
//            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
//        }
        String [] repid=array.split(",");
        ArrayList<String> list=new ArrayList<String>();
        ArrayList<String> temp=new ArrayList<String>();
        for(int i=0;i<repid.length;i++){
            boolean b=this.pro_isexist(id, repid[i]);
            if(b==true){
            String sql="insert into tb_process_rep (refid,repid,processid) values ("+com.getSeqStr("SEQ_PROCESS_REP")+","+repid[i]+","+id+")";
            list.add(sql);
            }
        }
        try {
            if(list.equals(temp)){
                res="ok";
            }
            else{
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public boolean pro_isexist(String processid,String repid){
        boolean b=false;
        DAO dao=new DAO();
        String sql="select * from tb_process_rep where processid="+processid+" and repid="+repid;
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                b=true;
            }
            else{
                b=false;
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }
    public boolean link_isexist(String linkid,String repid){
        boolean b=true;
        DAO dao=new DAO();
        String sql="select * from tb_process_rep where linkid="+linkid+" and repid="+repid;
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                b=true;
            }
            else{
                b=false;
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return b;
    }
    public boolean point_isexist(String pointid,String repid){
        boolean b=true;
         DAO dao=new DAO();
        String sql="select * from tb_process_rep where pointid="+pointid+" and repid="+repid;
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                b=true;
            }
            else{
                b=false;
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return b;
    }
     public String relate_process_delete(String array,String processid){
         String res="";
         DAO dao=new DAO();
         String [] repid=array.split(",");
         String [] myprocessid=processid.split("_");
         processid=myprocessid[myprocessid.length-1];
         ArrayList<String> list=new ArrayList<String>();
         for(int i=0;i<repid.length;i++){
             String sql="delete from tb_process_rep where repid="+repid[i]+" and processid="+processid;
             list.add(sql);
         }
        try {
            if(list.size()>0){
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
            }
            else{
                res="ok";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
         return res;
     }
     
      public String relate_process_delete(String array,String processidarray,String temp){
         String res="";
         DAO dao=new DAO();
         String [] repid=array.split(",");
         String [] processid=processidarray.split(",");
         ArrayList<String> list=new ArrayList<String>();
         for(int i=0;i<repid.length;i++){
             String sql="delete from tb_process_rep where repid="+repid[i]+" and processid="+processid[i];
             list.add(sql);
         }
        try {
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
         return res;
     }
    
    public String relate_link(String array,String id){
        String [] myid=id.split("_");
        id=myid[myid.length-1];
        String res="";
        String delete="delete from tb_link_rep where linkid="+id;
        DAO dao=new DAO();
        try {
            dao.execCommand(ConstUtil.DBNAME, delete);
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        String [] repid=array.split(",");
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0;i<repid.length;i++){
            String sql="insert into tb_link_rep (refid,repid,linkid) values ("+com.getSeqStr("SEQ_LINK_REP")+","+repid[i]+","+id+")";
            list.add(sql);
        }
        try {
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
     public String relate_link_delete(String array,String linkid){
         String res="";
         DAO dao=new DAO();
         String [] repid=array.split(",");
         String [] myprocessid=linkid.split("_");
         linkid=myprocessid[myprocessid.length-1];
         ArrayList<String> list=new ArrayList<String>();
         for(int i=0;i<repid.length;i++){
             String sql="delete from tb_link_rep where repid="+repid[i]+" and linkid="+linkid;
             list.add(sql);
         }
        try {
            if(list.size()>0){
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
            }
            else{
                res="ok";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
         return res;
     }
    
    public String relate_point(String array,String id){
        String [] myid=id.split("_");
        id=myid[myid.length-1];
        String res="";
        DAO dao=new DAO();
        String [] repid=array.split(",");
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0;i<repid.length;i++){
            String sql="insert into tb_point_rep (refid,repid,pointid) values ("+com.getSeqStr("SEQ_POINT_REP")+","+repid[i]+","+id+")";
            list.add(sql);
        }
        try {
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
     public String relate_point_delete(String array,String pointid){
         String res="";
         DAO dao=new DAO();
         String [] repid=array.split(",");
         String [] myprocessid=pointid.split("_");
         pointid=myprocessid[myprocessid.length-1];
         ArrayList<String> list=new ArrayList<String>();
         for(int i=0;i<repid.length;i++){
             String sql="delete from tb_point_rep where repid="+repid[i]+" and pointid="+pointid;
             list.add(sql);
         }
        try {
            if(list.size()>0){
            boolean b=dao.execCommand(ConstUtil.DBNAME, list);
            if(b==true){
                res="ok";
            }
            else{
                res="wrong";
            }
            }
            else{
                res="ok";
            }
        } catch (ServletException ex) {
            Logger.getLogger(RelateRep.class.getName()).log(Level.SEVERE, null, ex);
        }
         return res;
     }
}
